package lormi.meipin.service.impl;


import lormi.meipin.common.LMOAuth2API;
import lormi.meipin.dto.ChangePhoneDto;
import lormi.meipin.entity.*;
import lormi.meipin.exception.LMException;
import lormi.meipin.service.BaseService;
import lormi.meipin.service.ICheckCodeService;
import lormi.meipin.service.IMemberService;
import lormi.meipin.util.CacheUtil;
import lormi.meipin.util.StringUtil;
import lormi.meipin.util.TEncryptUtil;
import lormi.meipin.vo.LoginVo;
import lormi.meipin.vo.TellInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MemberService extends BaseService implements IMemberService{

	@Autowired
	private ICheckCodeService checkCodeService;

	@Override
	public Member getMember(Integer id,Integer type) throws LMException{
		Member member = (Member) CacheUtil.getCacheUtil().getLoginCache(id+"_"+type);
		if (member==null) {
			member = memberMapper.selectByIdAndUserType(id, type);
			CacheUtil.getCacheUtil().setLoginCache(id+"_"+type, member);
		}
		return member;
	}

	@Override
	public void checkMember(Integer id,Integer type) throws LMException{
		if (getMember(id, type)==null) {
			throw new LMException(150012);
		}
	}

	@Override
	public LoginVo checkMember(String phone) throws LMException{
		Member member = memberMapper.selectByPhone(phone);
		if (member==null) {
			return null;
		}
		String token = LMOAuth2API.getToken();
		Token t = tokenMapper.selectByUserId(member.getId());
		if (t==null) {
			if(tokenMapper.insert(new Token(member.getId(),token))<1){
				throw new LMException(100035);
			}
		}else{
			t.setUpdatetime(new Date());
			t.setExittime(new Date(t.getUpdatetime().getTime()+15*24*3600*1000));
			if(tokenMapper.update(t)<1){
				throw new LMException();
			}
		}
		
		return new LoginVo(member,token);
	}

	@Override
	public void changePhone(ChangePhoneDto changePhoneDto) throws LMException {
		//1. 验证邀请码
		checkCodeService.checkCodeVaild(changePhoneDto.getPhone(), Integer.valueOf(changePhoneDto.getCode()));
		
		//2. 判断新手机好是否存在
		if (memberMapper.selectByPhone(changePhoneDto.getPhone())!=null) {
			throw new LMException(120017);
		}
		Member member = memberMapper.selectByPrimaryKey(changePhoneDto.getUserid());
		if (member==null) {
			throw new LMException();
		}
		member.setMobile(changePhoneDto.getPhone());
		if (memberMapper.updateByPrimaryKey(member)<1) {
			throw new LMException(200005);
		}
	}

	@Override
	public List<TellInfoVo> getMemberNameAndPhoto(String listid) throws LMException {

		List<String> list = StringUtil.getSplit(listid.replace("[","").replace("]",""),",");
		
		List<TellInfoVo> re = new ArrayList<>();
		TellInfoVo tellInfoVo = null;
		for(String s:list){
			Integer id = Integer.valueOf(s);
			if(id==99999999){
				tellInfoVo = new TellInfoVo();
				tellInfoVo.setId(99999999);
				tellInfoVo.setName("通知");
				tellInfoVo.setPhone("http://v2.meipins.com/img/icons/no-chat.png");
				re.add(tellInfoVo);
				continue;
			}
			Member member = memberMapper.selectByPrimaryKey(id);
			if(member==null){
				tellInfoVo = new TellInfoVo();
				tellInfoVo.setId(id);
				tellInfoVo.setName("null");
				tellInfoVo.setPhone("http://pic.meipins.com//meipins/15.jpg");
			}else if(member.getUsertype()==1){
				Talent t = talentMapper.selectByMemId(id);
				tellInfoVo = new TellInfoVo();
				tellInfoVo.setId(t.getMemid());
				tellInfoVo.setName(t.getName());
				tellInfoVo.setPhone(t.getPhoto());
			}else if(member.getUsertype()==2){
				MerChant m = merChantMapper.selectByMerId(id);
				tellInfoVo = new TellInfoVo();
				tellInfoVo.setId(m.getMerid());
				tellInfoVo.setName(m.getName());
				tellInfoVo.setPhone(m.getPhoto());
			}
			re.add(tellInfoVo);
		}
		return re;
	}

	@Override
	public Member getMemberPhone(Integer userid,Integer usertype,Integer tellid) throws LMException {
		//1. 判断系统是否开启
		if (usertype==2 && "true".equals(systemInfoMapper.selectByKey("tell_boolean").getSvalue())) {
			throw new LMException(100038);
		}
		//2. 获取手机号并加密
		Member member = memberMapper.selectByPrimaryKey(tellid);
		if (member==null) {
			throw new LMException(200006);
		}
		//加密
		String phone = member.getMobile();
		String key = member.getId()+"";
		try {
			member.setMobile(TEncryptUtil.enBase64(TEncryptUtil.enAES(phone,key)));
		} catch (Exception e) {
			throw new LMException();
		}
		tellLogMapper.insert(new TellLog(userid,tellid));
		return member;
	}
	
	
}
